package com.cxrj.zhsq.interfaceManage.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.druid.support.json.JSONUtils;
import com.cxrj.zhsq.userManage.entity.AppUser;
import com.cxrj.zhsq.userManage.service.AppUserBlackService;
import com.cxrj.zhsq.userManage.service.AppUserService;
import com.cxrj.zhsq.userManage.service.DirectoriesService;
import com.cxrj.zhsq.userManage.service.DoorService;
import com.cxrj.zhsq.userManage.service.OrganizationService;
import com.cxrj.zhsq.userManage.service.SystemMessageService;
import com.cxrj.zhsq.utils.DoorUtil;
import com.cxrj.zhsq.utils.MessageUtil;

@RestController
@RequestMapping(value = "/interface")
public class InterfaceUserController {

	@Resource
	private AppUserService appUserService;

	@Resource
	private OrganizationService organizationService;

	@Resource
	private DirectoriesService directoriesService;

	@Resource
	private SystemMessageService systemMessageService;

	@Resource
	private DoorService doorService;

	@Resource
	private AppUserBlackService appUserBlackService;

	/**
	 * 用户注册
	 */
	@RequestMapping(value = "userRegist", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public Map<String, Object> regist(AppUser appUser, HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			map = appUserService.insertAppUser(appUser);
		} catch (Exception e) {
			map.put("result", "0");
			e.printStackTrace();
		}
		return map;
	}

	/**
	 * 短信验证
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "sendCode", produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public Map<String, Object> sendCode(String mobiles, String code) {
		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, Object> map1 = (Map<String, Object>) JSONUtils.parse(MessageUtil.login());
		map.put("returnMsg", MessageUtil.send(map1.get("token").toString(), mobiles, "[孝感市老市委大院智慧社区]您的验证码是《" + code + "》。请在APP中提交验证码完成验证。"));
		return map;
	}

	/**
	 * 用户登录
	 * 
	 * @return key : result 0：账号不存在 1：密码错误 2：登录成功
	 * @return key : appUser 用户登陆后用户个人信息（userId, 所属社区 communityName等）
	 */
	@RequestMapping(value = "userLogin", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public Map<String, Object> login(String userAccount, String userPassword, HttpServletRequest request) {
		return appUserService.selectLogin(userAccount, userPassword, request);
	}

	/**
	 * 修改用户
	 * 
	 * @return
	 */
	@RequestMapping(value = "userUpdate", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public Map<String, Object> userUpdate(AppUser appUser, HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			resultMap = appUserService.updateUser(appUser);
		} catch (Exception e) {
			resultMap.put("result", 0);
			e.printStackTrace();
		}
		return resultMap;
	}

	/**
	 * 用户修改密码
	 * 
	 * @param oldPass
	 *            原密码
	 * @param newPass
	 *            新密码
	 * @param userId
	 *            用户id
	 * @param request
	 * @return key :result 1.成功 0.报错失败 2.原密码不正确
	 * 
	 */
	@RequestMapping(value = "userModifyPass", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public Map<String, Object> userModifyPass(String oldPass, String newPass, String userId, HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			resultMap = appUserService.updatePass(oldPass, newPass, userId, request);
		} catch (Exception e) {
			resultMap.put("result", 0);
			e.printStackTrace();
		}
		return resultMap;
	}

	/**
	 * 忘记密码
	 * 
	 * @return
	 */
	@RequestMapping(value = "userForget", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public Map<String, Object> userForget(String newPass, String userMobile, HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			resultMap = appUserService.updateUserForget(newPass, userMobile);
		} catch (Exception e) {
			resultMap.put("result", 0);
			e.printStackTrace();
		}
		return resultMap;
	}

	/**
	 * 修改手机
	 * 
	 * @return
	 */
	@RequestMapping(value = "userModifyMobile", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public Map<String, Object> userModifyMobile(String pass, String userMobile, String userId, HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			resultMap = appUserService.updateUserMobile(pass, userMobile, userId);
		} catch (Exception e) {
			resultMap.put("result", 0);
			e.printStackTrace();
		}
		return resultMap;
	}

	@RequestMapping(value = "selectAppById", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public Map<String, Object> selectAppUserById(String userId) {
		return appUserService.selectAppUserById(userId);
	}

	/**
	 * 查询所有部门
	 * 
	 * @return
	 */
	@RequestMapping(value = "selectOrganization", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public Map<String, Object> selectOrganization(String name) {
		return organizationService.selectAllCommunityByName(name);
	}

	/**
	 * 根据id查询通讯录
	 * 
	 * @return
	 */
	@RequestMapping(value = "selectDirectories", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public Map<String, Object> selectDirectories(String id) {
		return directoriesService.selectDirectories(id);
	}

	/**
	 * 查询系统消息
	 * 
	 * @return
	 */
	@RequestMapping(value = "selectSystemMessage", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> selectSystemMessage(String userId, String dataIndex, String pageCount, HttpServletRequest request) {
		Map<String, Object> resultMap = systemMessageService.selectSystemMessage(userId, dataIndex, pageCount, request);
		return resultMap;
	}

	/**
	 * 根据id查询系统消息
	 * 
	 * @return
	 */
	@RequestMapping(value = "selectSystemMessageId", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> selectSystemMessageId(String id, String userId, HttpServletRequest request) {
		Map<String, Object> resultMap = systemMessageService.selectSystemMessageById(id, userId, request);
		return resultMap;
	}

	/**
	 * 查询门
	 * 
	 * @return
	 */
	@RequestMapping(value = "selectByDoor", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> selectByDoor(String userId) {
		return doorService.selectDoor(userId);
	}

	/**
	 * 开门
	 * 
	 * @return
	 */
	@RequestMapping(value = "openDoor", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> openDoor(String doorUuid) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("result", DoorUtil.openDoor(doorUuid));
		return resultMap;
	}

	/**
	 * 拉黑
	 */
	@RequestMapping(value = "insertAppUserBlack", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public Map<String, Object> insertAppUserBlack(String userId, String userBlackId, HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			map = appUserBlackService.insertAppUserBlack(userId, userBlackId);
		} catch (Exception e) {
			map.put("result", "0");
			e.printStackTrace();
		}
		return map;
	}

	/**
	 * 拉黑返回
	 */
	@RequestMapping(value = "deleteAppUserBlack", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public Map<String, Object> deleteAppUserBlack(String userId, String userBlackId, HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			map = appUserBlackService.deleteAppUserBlack(userId, userBlackId);
		} catch (Exception e) {
			map.put("result", "0");
			e.printStackTrace();
		}
		return map;
	}

	/**
	 * 查询拉黑列表
	 * 
	 * @return
	 */
	@RequestMapping(value = "selectAppUserBlack", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public Map<String, Object> selectAppUserBlack(String userId) {
		return appUserBlackService.selectAppUserBlack(userId);
	}
}
